package com.haikesi.api.excutionplan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Type;
import com.haikesi.api.excutionplan.dao.ExcutionPlanProcessDao;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanPO;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanProcessPO;
import com.haikesi.api.excutionplan.domain.vo.*;
import com.haikesi.api.excutionplan.service.IExcutionPlanProcessService;
import com.haikesi.exception.BusinessException;
import com.haikesi.util.ApcosApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class ExcutionPlanProcessServiceImpl implements IExcutionPlanProcessService {

    @Autowired
    ExcutionPlanProcessDao dao;


    @Override
    public List<ExcutionPlanProcessVO> getList(ExcutionPlanProcessSearchVO excutionPlanProcessSearchVO) {
        List<ExcutionPlanProcessVO> list = dao.getList(excutionPlanProcessSearchVO);
        return list;
    }

    @Override
    public ExcutionPlanProcessPO addExcutionPlanProcess(ExcutionPlanProcessAddVO e) {
        JSONObject jsonObject = ApcosApiUtils.addExcutionPlanProcess(e.getAccount(), e.getPlan_id(), e.getProcess_name(),
                e.getHas_condition(), e.getPre_trigger_time(), e.getMode_ids(), e.getMode_desc(), e.getDelay_trigger_time(),
                e.getTrigger_conditon_desc(), e.getExcutionConditionList(), e.getExcutionTimeConditionList(), e.getSer_order());

        ExcutionPlanProcessPO po = new ExcutionPlanProcessPO();
        po.preInsert();
        po.setPlatformId(e.getPlan_id());
        po.setExcutionPlanId(e.getPlan_id_local());
        po.setName(e.getProcess_name());
        po.setHasCondition(e.getHas_condition());
        if("0000".equals(jsonObject.get("resultCode"))){
            po.setPlatformProcessId(jsonObject.get("resultContent").toString());
        }
        if(!e.getPre_trigger_time().isEmpty()){
            po.setPreTriggerTime(Integer.parseInt(e.getPre_trigger_time()));
        }
        if(!e.getDelay_trigger_time().isEmpty()){
            po.setDelayTriggerTime(Integer.parseInt(e.getDelay_trigger_time()));
        }
        po.setModeIds(e.getMode_ids());
        po.setTriggerConditonDesc(e.getTrigger_conditon_desc());
        po.setModeDesc(e.getMode_desc());
        po.setExcutionConditionList(e.getExcutionConditionList());
        po.setExcutionTimeConditionList(e.getExcutionTimeConditionList());

        int i = dao.insertSelective(po);
        return po;
    }

    @Override
    public void updateExcutionPlanProcess(ExcutionPlanProcessUpdateVO e) {
        JSONObject jsonObject = ApcosApiUtils.updateExcutionPlanProcess(e.getAccount(), e.getPlan_id(), e.getProcess_name(),
                e.getHas_condition(), e.getPre_trigger_time(), e.getMode_ids(), e.getMode_desc(), e.getDelay_trigger_time(),
                e.getTrigger_conditon_desc(), e.getExcutionConditionList(), e.getExcutionTimeConditionList(), e.getSer_order(),
                e.getPlatform_process_id());

        ExcutionPlanProcessPO po = new ExcutionPlanProcessPO();
        po.preUpdate();
        po.setId(e.getId());
        po.setPlatformId(e.getPlan_id());
        po.setExcutionPlanId(e.getPlan_id_local());
        po.setName(e.getProcess_name());
        po.setHasCondition(e.getHas_condition());
        if("0000".equals(jsonObject.get("resultCode"))){
            po.setPlatformProcessId(jsonObject.get("resultContent").toString());
        }
        if(!e.getPre_trigger_time().isEmpty()){
            po.setPreTriggerTime(Integer.parseInt(e.getPre_trigger_time()));
        }
        if(!e.getDelay_trigger_time().isEmpty()){
            po.setDelayTriggerTime(Integer.parseInt(e.getDelay_trigger_time()));
        }
        po.setModeIds(e.getMode_ids());
        po.setTriggerConditonDesc(e.getTrigger_conditon_desc());
        po.setModeDesc(e.getMode_desc());
        po.setExcutionConditionList(e.getExcutionConditionList());
        po.setExcutionTimeConditionList(e.getExcutionTimeConditionList());

        int i = dao.updateByPrimaryKeySelective(po);
    }

    @Override
    public void delete(ExcutionPlanProcessDeleteVO vo) {
        JSONObject jsonObject = ApcosApiUtils.deleteExcutionPlanProcess(vo.getAccount(), vo.getId());
        if("0000".equals(jsonObject.get("resultCode"))){
            ExcutionPlanProcessPO po = new ExcutionPlanProcessPO();
            po.preUpdate();
            po.setId(vo.getLocalId());
            po.setDelFlag(true);
            int i = dao.updateByPrimaryKeySelective(po);
        }
    }
}
